﻿using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using YogaStudio.Data;
using YogaStudio.Data.Domain;

namespace YogaStudio.Service.BLL
{
    public class CustomerService : ICustomerService
    {
        IUserManagementService userManager = new UserManagementService();

        private YogaStudioEntities db;

        public CustomerService()
        {
            db = new YogaStudioEntities();
        }
        public List<Customer> GetAllCustomers()
        {
            return db.Customers.ToList();
        }
        public Customer GetCustomer(int id)
        {
            return db.Customers.Find(id);

        }
        public Customer GetCustomerByUserId(int id)
        {
            return db.Customers.FirstOrDefault(w => w.UserId == id);

        }
        public bool DeleteCustomer(Customer customer)
        {
            if (customer == db.Customers.Remove(customer))
            {
                db.SaveChanges();
            }
            return true;

        }
        public void UpdateCusotmer(Customer customer)
        {
            db.Entry(customer).State = EntityState.Modified;
            db.SaveChanges();
        }
        public void SaveCustomer(Customer customer)
        {
            db.Customers.Add(customer);
            db.SaveChanges();
        }
        public void Dispose(bool disposing)
        {
            if (disposing) db.Dispose();
        }

        public Customer GetCustomerByLogInUserName(string username)
        {
            var user = userManager.GetUser(username);
            return GetCustomerByUserId(user.UserId);
        }

        public IDictionary<Section, ICollection<Customer>> GetStudentsByFacultyId(int? facultyId)
        {
            using (var sectionService = new SectionService())
            {
                return sectionService.GetSectionsOfNextSemester(facultyId)
                    .ToList()
                    .ToDictionary(key => key, value => value.Customers);
            }
        }
    }
}
